﻿KISSY.add("fgt/searchsuggestion", function (S, params) {
    var D = S.DOM,
        All = S.all;

    function SearchSuggestion(X) {
        SearchSuggestion.superclass.constructor.call(this, X || {});
        var self = this;
        self.q = S.one(self.get("q"));
        self.form = S.one(self.get("form"));
        self.sourceUrl = self.get("sourceUrl");
        self.portalUrl = self.get("portalUrl");
        if (!self.q || !self.form) {
            return
        }
        self.ON = true;

        S.use("suggest", function () {
            self._init();
        });
    }

    S.extend(SearchSuggestion, S.Base);
    SearchSuggestion.ATTRS = {
        q: { value: params },
        form: { value: params },
        sourceUrl: { value: params },
        portalUrl: { value: params },
        sugConfig: { value: { callbackFn: "qs", charset: 'gbk', containerWidth: '299px' } }
    };

    S.augment(SearchSuggestion, S.EventTarget, {
        _init: function () {
            var self = this;
            self.suggest = new S.Suggest(self.q, self.sourceUrl, self.get("sugConfig"));

            self._bind();
        },

        _bind: function () {
            var self = this,
                suggest = self.suggest;

            suggest.on("beforeStart", function (event) {
                return self.ON && (self._loading = true)
            });

            suggest.on("dataReturn", function (event) {
                if (this.returnedData.Result !== 0) {
                    return false
                }

                self.rawData = this.returnedData;
                self._loading = false;
            });

            suggest.on("beforeShow", self._beforeShow, self);
            suggest.on("beforeSubmit", self._beforeSubmit, self);
        },

        _beforeShow: function () {
            var self = this,
                suggest = self.suggest,
                sugitems = self.rawData,
                htmlstr = "";

            S.each(sugitems.Content, function (sugitem, i) {
                if (sugitem.Type == 1) {
                    htmlstr += '<li key="' + sugitem.Name + '" class="ks-odd" shopid="' + sugitem.ShopID + '"> <span class="ks-suggest-key">' + sugitem.Name + '</span> <span class="ks-suggest-result">' + sugitem.Region + '</span> </li>';
                } else {
                    htmlstr += '<li key="' + sugitem.Name + '" class="ks-odd"> <span class="ks-suggest-key">' + sugitem.Name + '</span> <span class="ks-suggest-result"> 约' + sugitem.Count + '条</span> </li>';
                }
            });

            if (htmlstr) {
                var finalhtmlstr = '<ol>' + htmlstr + '</ol>';
                D.prepend(D.create(finalhtmlstr), suggest.content);
            }
        },

        _beforeSubmit: function () {
            var self = this,
                suggest = self.suggest,
                qboxval = suggest.textInput.value,
                query = suggest.query,
                result = false,
                form = self.form,
                selopt = suggest.selectedItem;

            if (self._loading) { return }

            if (selopt && selopt.className) {
                var parent = selopt.parentNode,
                    opts = D.query("li", parent),
                    index = 1;
                S.each(opts, function (opt, i) {
                    if (opt == selopt) {
                        index = i + 1;
                        return false
                    }
                });

                var shopid = All(selopt).attr("shopid");
                if (shopid) {
                    var shopurl = self.portalUrl + shopid + "?source=suggest";
                    location.href = shopurl;
                    return false
                }

                addtoform("suggest", "0_" + index, form);
                result = true
            }
            if (result) {
                addtoform("source", "suggest", form);
                if (qboxval != query) { addtoform("wq", query, form) }
            }
        }
    });

    function addtoform(name, value, form) {
        if (!form) { return }
        var input = form[0][name];
        if (input) {
            D.val(input, value);
        } else {
            input = D.create('<input type="hidden" name="' + name + '" value="' + value + '" />');
            form.append(input);
        }

        return input;
    }

    S.FGT.SearchSuggestion = SearchSuggestion;
    return SearchSuggestion;
});